// pages/home/home.js
let {
    getHomeData,
    recommendBlank,
    homeHisearch
} = require("../../api/home")
// 我的数据库数据
let {
    getcommodity,
    getcommodityData,
    getgoodsLists,
} = require("../../api/home")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        swiperHeight: 200,
        imgs: [
            "https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/51b3f561b860023b9739c9ef2ad4be9e.jpg",
            "https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/d80ff6ad33796a3c5eb7434fe5fb00a0.jpg?f=webp&w=1080&h=540&bg=F5EEE6",
            "https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/51b3f561b860023b9739c9ef2ad4be9e.jpg",
            "https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/51b3f561b860023b9739c9ef2ad4be9e.jpg",
            "https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/51b3f561b860023b9739c9ef2ad4be9e.jpg",
        ],
        current: 0,
        category: [{
                img: "/image/01_03.gif",
                text: "小米上新"
            },
            {
                img: "/image/01_05.gif",
                text: "手机"
            },
            {
                img: "/image/01_07.gif",
                text: "k50系列"
            },
            {
                img: "/image/01_09.gif",
                text: "电视"
            },
            {
                img: "/image/01_11.gif",
                text: "大家电"
            },
            {
                img: "/image/01_18.gif",
                text: "米金商城"
            },
            {
                img: "/image/01_19.gif",
                text: "小米众筹"
            },
            {
                img: "/image/01_20.gif",
                text: "电脑"
            },
            {
                img: "/image/01_21.gif",
                text: "生活电器"
            },
            {
                img: "/image/01_22.png",
                text: "智能家居"
            },
        ],
        conIndex: 0,
        swiper_height: "200px",

        //   定义的数据
        goodsLists: null,

        // //   自己写的接口
        commodity: []


    },

    // 商品 选项卡切换
    setConIndex(event) {
        console.log(event.currentTarget.dataset.id);
        let index = event.currentTarget.dataset.id
        this.setData({
            conIndex: index,
        })

        this.getNavData(index);
    },
    // 轮播图商品切换
    setSwiperGoodsIndex(event) {
        console.log("触发效果", event.detail.current);
        this.setData({
            conIndex: event.detail.current,
        })
        this.autoHeight();
    },

    // 获取 手机 家电 数据
    getNavData(index) {
        // console.log("api的index",this.data.goodsLists[index] );
        if (this.data.goodsLists[index].lists.length == 0) { //没有数据才请求数据
            // 调用方法
            this.gteListData(index)
        }
    },


    // 自定义方法
    gteListData(index_) {
        let QueryData = {
            "pageNum": 1,
            "pageSize": 10,
            "type": this.data.goodsLists[index_].name
        }
        console.log("自定义方法",QueryData);
        getcommodityData(QueryData).then((data) => {
            console.log("数据库返==>回手机家电等", data);
            let list = data.data.list
            let listsArr = [];
            list.forEach(item => {
                listsArr.push({
                    id: item.id,
                    img: JSON.parse('[' + item.cover + ']').map(items => items.url).toString(), //数据处理
                    name: item.name,
                    price: item.price,
                    priceOld: (item.price + new Number(item.discount))
                })
            })
            // console.log("数据库返回数据列表==>", listsArr);
            this.setData({
                ["goodsLists[" + index_ + "].lists"]: listsArr
            })


        }).then(() => {
            this.autoHeight();
        })
    },

    // 计算内容高度
    autoHeight() {

        let {
            conIndex
        } = this.data;

        wx.createSelectorQuery()
            .select('#end' + conIndex).boundingClientRect()
            .select('#start' + conIndex).boundingClientRect().exec(rect => {
                console.log("rect", rect);
                let _space = rect[0].top - rect[1].top; //end高度-start高度=差值
                _space = _space + 'px';
                this.setData({
                    swiper_height: _space
                });

            })

    },

    // 轮播图指示点
    setSwiperIndex(event) {
        console.log("触发效果", event.detail.current);
        // this.setData 修改data中数据,数据修改后更新视图
        this.setData({
            current: event.detail.current
        })
    },

    // 获取图片高度
    bindloadImg(e) {
        let {
            height,
            width
        } = e.detail;
        // 获取硬件系统信息
        wx.getSystemSetting({
            success: (res) => {
                this.setData({
                    swiperHeight: res.windowWidth * 0.95 / width * height
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        // this.autoHeight();
        // 10.30添加数据，获取goodsLists数据
        getgoodsLists().then(data => {
            let infoData = data.data
            console.log("infoData",infoData);
            let list_ = []
            infoData.forEach(item => {
                // 标记有0的push进去
                if(JSON.parse('[' + item.display + ']')[0].indexOf(0)!=-1){
                    // console.log(item);
                    list_.push({
                        id: item.id,
                        name: item.name,
                        title: item.title,
                        lists: []
                    })
                }

            })
            this.setData({
                goodsLists: list_
            })
            this.gteListData(0) //传入参数

            console.log("列表数据返回", list_);
        }).then(() => {
            this.autoHeight();
        })







        //   这是初始精选数据
        getHomeData().then(data => {
            let sections = data.data.data.sections; //轮播图
            this.setData({
                imgs: sections[0].body.items
            })
        })



        // this.getNavData(this.conIndex)


        // recommendBlank().then(data => {
        //     console.log(data);
        //     let recom_list = data.data.recom_list;
        //     console.log(recom_list)
        //     let listsArr = [];
        //     recom_list.forEach(item => {
        //         listsArr.push({
        //             id: item.action.path,
        //             img: item.image_url,
        //             name: item.name,
        //             price: item.price,
        //             priceOld: item.market_price
        //         })
        //     })
        //     this.setData({
        //         "goodsLists[0].lists": listsArr
        //     })
        //     //   console.log("listsArr",listsArr);
        // }).then(() => {
        //     this.autoHeight();
        // })








        // 列表下的数据(测试)
        getcommodity().then(data => {
            let listData = data.data
            // 将传来的字符串改为数组对象
            listData.forEach(item => {
                item.type = JSON.parse(item.type) //转为数组
                item.cover = JSON.parse('[' + item.cover + ']')
                item.petaliImage = JSON.parse('[' + item.petaliImage + ']')
                item.produceImage = JSON.parse('[' + item.produceImage + ']')
            });


            console.log("后台数据", listData);
            this.setData({
                commodity: listData
            })
        })



    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        console.log("onReady");
        // this.autoHeight();
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        console.log("onShow");
        // this.autoHeight();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {
        console.log("onHide");

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})